﻿
// TestDrawView.cpp: CTestDrawView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "TestDraw.h"
#endif

#include "TestDrawDoc.h"
#include "TestDrawView.h"
#include "AddDrawDlg.h"
#include "MoveDrawDlg.h"
#include "CGraph.h"
#include "MyRectangle.h"
#include "MyTrangle.h"
#include "MyEllipse.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTestDrawView

IMPLEMENT_DYNCREATE(CTestDrawView, CView)

BEGIN_MESSAGE_MAP(CTestDrawView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_COMMAND(IDM_DELETE, &CTestDrawView::OnDelete)
	ON_COMMAND(IDM_MOVE, &CTestDrawView::OnMove)
	ON_COMMAND(IDM_ADD, &CTestDrawView::OnAdd)
END_MESSAGE_MAP()

// CTestDrawView 构造/析构

CTestDrawView::CTestDrawView() noexcept
{
	// TODO: 在此处添加构造代码
	//m_iOffsetX = 0;
	//m_iOffsetY = 0;
}

CTestDrawView::~CTestDrawView()
{
	CTestDrawDoc* pDoc = GetDocument();
	for (int i = 0; i < pDoc->m_ptrArray.GetSize(); ++i)
	{
		if (nullptr != pDoc->m_ptrArray[i]) {
			delete pDoc->m_ptrArray[i];
			pDoc->m_ptrArray[i] = nullptr;
		}
	}
	pDoc->m_ptrArray.RemoveAll();
}

BOOL CTestDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CTestDrawView 绘图

void CTestDrawView::OnDraw(CDC* pDC)
{
	CTestDrawDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
	//CClientDC dc(this);
	CBrush* pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
	pDC->SelectObject(pBrush);

	for (int i = 0; i < pDoc->m_ptrArray.GetCount(); ++i)
	{
		CGraph* graph = (CGraph*)pDoc->m_ptrArray.GetAt(i);
		graph->Draw(pDC);
		//CPoint ptBegin(graph->m_ptCenter.x - graph->m_iOffsetDistance, graph->m_ptCenter.y - graph->m_iOffsetDistance);
		//CPoint ptEnd(graph->m_ptCenter.x + graph->m_iOffsetDistance, graph->m_ptCenter.y + graph->m_iOffsetDistance);
		//switch (graph->m_iDrawType)
		//{
		//case 0:
		//{
		//	CPoint pt[3];
		//	pt[0] = CPoint(graph->m_ptCenter.x, graph->m_ptCenter.y - graph->m_iOffsetDistance);
		//	pt[1] = CPoint(graph->m_ptCenter.x - graph->m_iOffsetDistance, graph->m_ptCenter.y + graph->m_iOffsetDistance);
		//	pt[2] = CPoint(graph->m_ptCenter.x + graph->m_iOffsetDistance, graph->m_ptCenter.y + graph->m_iOffsetDistance);
		//	dc.Polygon(pt, 3);
		//	break;
		//}
		//case 1:
		//{
		//	dc.Rectangle(CRect(ptBegin, ptEnd));
		//	break;
		//}
		//case 2:
		//{
		//	dc.Ellipse(CRect(ptBegin, ptEnd));
		//	break;
		//}
		//default:
		//	break;
		//}
	}
}


// CTestDrawView 打印

BOOL CTestDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CTestDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CTestDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CTestDrawView 诊断

#ifdef _DEBUG
void CTestDrawView::AssertValid() const
{
	CView::AssertValid();
}

void CTestDrawView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTestDrawDoc* CTestDrawView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDrawDoc)));
	return (CTestDrawDoc*)m_pDocument;
}
#endif //_DEBUG


// CTestDrawView 消息处理程序


void CTestDrawView::OnDelete()
{
	// TODO: 在此添加命令处理程序代码
	CTestDrawDoc* pDoc = GetDocument();
	pDoc->m_ptrArray.RemoveAll();
	Invalidate();
}


void CTestDrawView::OnMove()
{
	// TODO: 在此添加命令处理程序代码
	MoveDrawDlg dlg;
	if (IDOK == dlg.DoModal()) {
		CTestDrawDoc* pDoc = GetDocument();
		//pDoc->m_iOffsetX = dlg.m_iOffsetX;
		//pDoc->m_iOffsetY = dlg.m_iOffsetY;
		for (int i = 0; i < pDoc->m_ptrArray.GetCount(); ++i)
		{
			CGraph* graph = (CGraph*)pDoc->m_ptrArray.GetAt(i);
			graph->Move(dlg.m_iOffsetX, dlg.m_iOffsetY);
			//graph->m_ptCenter.x += pDoc->m_iOffsetX;
			//graph->m_ptCenter.y += pDoc->m_iOffsetY;
		}
		Invalidate();
	}

}


void CTestDrawView::OnAdd()
{
	// TODO: 在此添加命令处理程序代码
	AddDrawDlg dlg;
	if (IDOK == dlg.DoModal()) {

		//CClientDC dc(this);
		//CBrush* pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
		//dc.SelectObject(pBrush);
		//CPoint ptBegin(dlg.m_CenterX - dlg.m_OffsetDistance, dlg.m_CenterY - dlg.m_OffsetDistance);
		//CPoint ptEnd(dlg.m_CenterX + dlg.m_OffsetDistance, dlg.m_CenterY + dlg.m_OffsetDistance);

		CGraph* graph = nullptr;
		switch (dlg.m_DrawType)
		{
		case 0:
		{
			//CPoint pt[3];
			//pt[0] = CPoint(dlg.m_CenterX, dlg.m_CenterY - dlg.m_OffsetDistance);
			//pt[1] = CPoint(dlg.m_CenterX - dlg.m_OffsetDistance, dlg.m_CenterY + dlg.m_OffsetDistance);
			//pt[2] = CPoint(dlg.m_CenterX + dlg.m_OffsetDistance, dlg.m_CenterY + dlg.m_OffsetDistance);
			//dc.Polygon(pt, 3);
			graph = new MyTrangle(dlg.m_DrawType, CPoint(dlg.m_CenterX, dlg.m_CenterY), dlg.m_OffsetDistance);
			break;
		}
		case 1:
		{
			//dc.Rectangle(CRect(ptBegin, ptEnd));
			graph = new MyRectangle(dlg.m_DrawType, CPoint(dlg.m_CenterX, dlg.m_CenterY), dlg.m_OffsetDistance);
			break;
		}
		case 2:
		{
			//dc.Ellipse(CRect(ptBegin, ptEnd));
			graph = new MyEllipse(dlg.m_DrawType, CPoint(dlg.m_CenterX, dlg.m_CenterY), dlg.m_OffsetDistance);
			break;
		}
		default:
			break;
		}
		CTestDrawDoc* pDoc = GetDocument();
		pDoc->m_ptrArray.Add(graph);
	}
	Invalidate();
}
